package com.sinocare.project.pharmacy.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.common.entity.SccDrugstore;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.pharmacy.db.SccDrugstoreDo;
import com.sinocare.project.pharmacy.service.SccDrugstoreService;
import com.sinocare.project.pharmacy.vo.SccDrugstoreVo;
import com.sinocare.project.pharmacy.vo.SccSDrugstoreVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 连锁药店表(SccDrugstore)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:52:12
 */
@RestController
@RequestMapping("sccDrugstore")
@CrossOrigin
public class SccDrugstoreController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccDrugstoreService sccDrugstoreService;

    /**
     * 分页查询总店所有数据
     *
     * @param page         分页对象
     * @param data 查询实体JSON
     * @return 所有数据
     */
    @GetMapping
    public Result selectParentAll(Integer page,Integer limit, String data) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(data)){
            SccDrugstoreVo sccDrugstoreVo = JSON.parseObject(data, SccDrugstoreVo.class);
            wrapper.like(!StrUtil.hasBlank(sccDrugstoreVo.getStoreName()),"store_name",sccDrugstoreVo.getStoreName());
            wrapper.likeRight(!StrUtil.hasBlank(sccDrugstoreVo.getStoreCode()),"store_code",sccDrugstoreVo.getStoreCode());
        }
        wrapper.eq("parent_code",0);
        PageHelper.startPage(page,limit);
        List<SccDrugstoreDo> tests = sccDrugstoreService.tests(wrapper);
        PageInfo<SccDrugstoreDo> info = new PageInfo<>(tests);
        return Result.ok(ResultCode.SUCCESS,info.getTotal(),info.getList());
    }
    /**
     * 分页查询分店所有数据
     *
     * @param page         分页对象
     * @param searchParams 查询实体JSON
     * @return 所有数据
     */
    @GetMapping("/sub")
    public Result selectAll(Integer page,Integer limit, String searchParams) {
        QueryWrapper<SccDrugstore> wrapper = new QueryWrapper<>();
        SccSDrugstoreVo sccSDrugstoreVo=new SccSDrugstoreVo();
        if (!StrUtil.hasBlank(searchParams)){
            sccSDrugstoreVo = JSON.parseObject(searchParams, SccSDrugstoreVo.class);
            wrapper.like(!StrUtil.hasBlank(sccSDrugstoreVo.getStoreName()),"store_name",sccSDrugstoreVo.getStoreName());
            wrapper.likeRight(!StrUtil.hasBlank(sccSDrugstoreVo.getStoreCode()),"store_code",sccSDrugstoreVo.getStoreCode());
            wrapper.likeRight(!StrUtil.hasBlank(sccSDrugstoreVo.getProvince()),"province_code",sccSDrugstoreVo.getProvince());
            wrapper.likeRight(!StrUtil.hasBlank(sccSDrugstoreVo.getCity()),"city_code",sccSDrugstoreVo.getCity());
            wrapper.likeRight(!StrUtil.hasBlank(sccSDrugstoreVo.getCounty()),"county_code",sccSDrugstoreVo.getCounty());
        }
        wrapper.eq("parent_code",sccSDrugstoreVo.getParentCode());
        PageHelper.startPage(page,limit);
//        List<SccDrugstoreDo> tests = sccDrugstoreService.tests(wrapper);
        List<SccDrugstore> list = this.sccDrugstoreService.list(wrapper);
        PageInfo info = new PageInfo<>(list);
        return Result.ok(ResultCode.SUCCESS,info.getTotal(),info.getList());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        QueryWrapper<SccDrugstore> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", id);
        List<SccDrugstore> list = this.sccDrugstoreService.list(wrapper);
        return Result.ok(ResultCode.SUCCESS,list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("role/{id}")
    public Result selectRole(@PathVariable Integer id) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        wrapper.eq("c.id",id);
        SccDrugstore byUserId = this.sccDrugstoreService.getByUserId(wrapper);
        return Result.ok(ResultCode.SUCCESS,byUserId);
    }

    /**
     * 新增数据
     *
     * @param data 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SccDrugstore data) {
        QueryWrapper<SccDrugstore> wrapper = new QueryWrapper<>();
        wrapper.select("MAX(store_code) as storeCode");
        SccDrugstore one = this.sccDrugstoreService.getOne(wrapper);
        int i = Integer.parseInt(one.getStoreCode())+1;
        data.setStoreCode(""+i);
        Date date = DateUtil.date();
        data.setCreateTime(date);
        data.setUpdateTime(date);
        return Result.ok(ResultCode.SUCCESS,this.sccDrugstoreService.save(data));
    }

    /**
     * 修改数据
     *
     * @param sccDrugstore 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SccDrugstore sccDrugstore) {
        return Result.ok(ResultCode.SUCCESS,this.sccDrugstoreService.updateById(sccDrugstore));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS,this.sccDrugstoreService.removeByIds(idList));
    }
}

